package com.zzptc.myk.myapplication;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.os.Bundle;
import android.util.Log;

import com.zzptc.myk.myapplication.Service.Myservice;
import com.zzptc.myk.myapplication.adapter.MyRvAdapter;
import com.zzptc.myk.myapplication.databinding.ActivityMainBinding;
import com.zzptc.myk.myapplication.entity.RecipeModel;
import com.zzptc.myk.myapplication.entity.ResultsBean;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainActivity extends AppCompatActivity {
    //全局 每次都需要获取 放在全局方便
    Retrofit retrofit=new Retrofit.Builder()
            .baseUrl("https://liyuankun.cn/")
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    Myservice myservice = retrofit.create(Myservice.class);
    private static final String TAG = "MainActivity";
    ActivityMainBinding binding;//将binding赋值给全局变量

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //DataBinding数据绑定 绑定小界面
       binding=DataBindingUtil.setContentView(this,R.layout.activity_main);
       //binding.rv
        //RecyclerView rv = findViewById(R.id.rv_id);
        getAllRecipe();//第一步  存数据
    }

    private void getAllRecipe() {

        Call<RecipeModel> all = myservice.getAll(10);
        all.enqueue(new Callback<RecipeModel>() {
            @Override
            public void onResponse(Call<RecipeModel> call, Response<RecipeModel> response) {
                List<ResultsBean> results = response.body().getResults();
                for (ResultsBean r:
                     results) {
                    r.save();
                }

                MyRvAdapter adapter=new MyRvAdapter(MainActivity.this);
                LinearLayoutManager manager = new LinearLayoutManager(MainActivity.this);
                binding.rv.setLayoutManager(manager);

                binding.rv.setAdapter(adapter);//适配器
            }

            @Override
            public void onFailure(Call<RecipeModel> call, Throwable t) {
                Log.e(TAG, "onFailure: ",t );
            }
        });

    }
}
